<?php

/**
 * Class OrderToday
 * 今日订单
 */
   Class OrderToday extends \LaravelBook\Ardent\Ardent{
       protected $table='orders_today';

       public $fillable=['status','order_id'];
       public $timestamps = false;

       //更新表单状态 返回1 or 0
       public static function updateStatus($arrInput){
           $order_today_id=array_get($arrInput,'order_today_id',0);
           $status=array_get($arrInput,'status',0);

          if($status&&$order_today_id)
          {
              $order=OrderToday::find($order_today_id);
              if($order&&($status=='1'||$status=='2')){
                  $order->status=$status;
                  if($order->save()){
                      if($order->status==2){

                         Order::find($order->order_id)->update(array('status'=>4));
                      }
                      return 1;
                  }else{
                      return 0;
                  }
              }else{
                  return 0;
              }
          }else{
              return 0;
          }
       }

       //获取列表
       public static function getList($arrInput)
       {
           $entries=self::join('orders','orders_today.order_id','=','orders.id')
                    ->select('orders_today.id','orders.id as order_id','orders.total_price','orders.created_at');//'order_foods.price','order_foods.count'
           if(array_get($arrInput,'status')){
               $entries=$entries->where('orders_today.status',array_get($arrInput,'status'));
           }
           else
           {
              $entries=$entries->where('orders_today.status',1);
           }
           $entries=$entries->get();
           $order_ids=$entries->lists('order_id');

           if(!empty($order_ids)){
               $foods=OrderFood::whereIn('order_id',$order_ids)->select('name','price','count','order_id')->get();
           }else{
               $foods = [];
           }

           foreach($entries as $entry){
               $arr=array();
               foreach($foods as $food){
                   if($entry->order_id==$food->order_id){
                       $arr[]=$food;
                   }
               }
               $entry->foods=$arr;
           }
           return $entries;
       }

   }